package com.explorati.LeetCode125.validpalindrome;

/**
 * 125. Valid Palindrome 回文串：正着看和反着看是同样的 
 * 回文可以是数字也可以是字母  以下方法适用于中包含数字
 * 
 * Given a string, determine if it is a palindrome, considering only
 * alphanumeric characters and ignoring cases.
 * 
 * Note: For the purpose of this problem, we define empty string as valid
 * palindrome.
 * 
 * @author explorati
 *
 */
public class Solution {
	public static boolean isPalindrome(String s) {
		if (s == "") {
			return true;
		}
		boolean flag = false;
		String str = s.toLowerCase();
		int i = 0, j = s.length() - 1;
		while (i <= j) {
			while (i < str.length() && (str.charAt(i) < 'a' || str.charAt(i) > 'z')) {
				i++;
			}
			while (j >= 0 && (str.charAt(j) < 'a' || str.charAt(j) > 'z')) {
				j--;
			}
			if (i >= j) {
				return true;
			}
			if (str.charAt(i) != str.charAt(j)) {
				return false;
			}
			i++;
			j--;
		}
		return flag;
	}

	public static void main(String[] args) {
		String s = "A man, a plan, a canal: Panama";
		// String s = "race a car";
		// String s = "0P";

		System.out.println(isPalindrome(s));
	}
}
